﻿Imports System.Data.SqlClient
Imports dropbox.Form1

Public Class LoginForm1
    Public mainForm As Form1
    Public loggedon As Boolean = False
    ' TODO: Insert code to perform custom authentication using the provided username and password 
    ' (See http://go.microsoft.com/fwlink/?LinkId=35339).  
    ' The custom principal can then be attached to the current thread's principal as follows: 
    '     My.User.CurrentPrincipal = CustomPrincipal
    ' where CustomPrincipal is the IPrincipal implementation used to perform authentication. 
    ' Subsequently, My.User will return identity information encapsulated in the CustomPrincipal object
    ' such as the username, display name, etc.
    Public logininfo As New LoginInformation
    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        'Dim checkloginsql As New SqlDataAdapter
        'checkloginsql.SelectCommand = New SqlCommand("SELECT * FROM [DocumentManagement].[dbo].[users] WHERE USERNAME = '" & UsernameTextBox.Text & "' AND PASSWORD = '" & PasswordTextBox.Text & "'")
        Dim con As New SqlConnection
        Dim reader As SqlDataReader

        con.ConnectionString = mainForm.settingsdialog.txtconstr.Text
        Dim cmd As New SqlCommand("SELECT * FROM [DocumentManagement].[dbo].[users] WHERE USERNAME = '" & UsernameTextBox.Text & "' AND PASSWORD = '" & PasswordTextBox.Text & "'", con)
        con.Open()
        'Console.WriteLine("Connection Opened")
        reader = cmd.ExecuteReader()
        While reader.Read()
            'Console.WriteLine(String.Format("{0}, {1}", reader(0), reader(1)))
            If reader(0) <> Nothing Then
                loggedon = True
                lblcheck.Text = "Login Success"

                logininfo.username = reader(0)
                logininfo.email = reader(2)
                logininfo.company = reader(3)
                logininfo.department = reader(4)
                logininfo.role = reader(5)

                If CheckBox1.Checked = True Then
                    My.Settings.username = reader(0)
                    My.Settings.password = reader(1)
                    My.Settings.email = reader(2)
                    My.Settings.company = reader(3)
                    My.Settings.department = reader(4)
                    My.Settings.role = reader(5)
                Else
                    My.Settings.username = ""
                    My.Settings.password = ""
                    My.Settings.email = ""
                    My.Settings.company = ""
                    My.Settings.department = ""
                    My.Settings.role = ""
                End If
                My.Settings.rootdir = mainForm.settingsdialog.txtroot.Text
                My.Settings.Save()
                Me.Close()
                Exit While
            End If
        End While
        If loggedon = False Then
            loggedon = False
            lblcheck.Text = "Incorrect Username or Password"
        End If
        con.Close()
    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Me.Close()
    End Sub

    Private Sub LoginForm1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub


End Class
